

import de.medieninf.sensor.SensData;
import de.medieninf.sensor.SensorReceiver;
import de.medieninf.sensor.SensorReceiverListener;

public class SampleReceiveMany {
	private static class SampleListener implements SensorReceiverListener {
		public void handle(SensData sensData) {
			Object[] args = {
					Double.valueOf(sensData.getX()),
					Double.valueOf(sensData.getY()),
					Double.valueOf(sensData.getZ()),
			        sensData.getKeys(),
			        Double.valueOf(sensData.getTimestamp()),
			        sensData.getDeviceId()};
			System.out.format("{x:%9.4f, y:%9.4f, z:%9.4f, k:%8s, timestamp:%10.2f, devid: %s}\n", args);
		}
	}
	
	public static void main(String[] argv) {
		int receiveTime = 60000;
		int[] ports = {9642, 9643, 9644};
		if (argv.length > 0) {
			ports = new int[argv.length];
			for (int i=0; i < argv.length; i++) {
				ports[i] = Integer.parseInt(argv[i]);
			}
		}
		SensorReceiver[] sensorReceivers = new SensorReceiver[ports.length];
		System.out.print("Receives for " + receiveTime + " milliseconds on ports ");
		for (int i=0; i < ports.length; i++) {
			sensorReceivers[i] = new SensorReceiver(ports[i]);
			sensorReceivers[i].setListener(new SampleListener());
			System.out.print(ports[i] + " ");
		}
		System.out.println(" (change with commandline)");
		System.out.println("Receiving for " + receiveTime/1000 + " seconds.");
		for (int i=0; i < ports.length; i++) {
			sensorReceivers[i].startReceiving();
		}
		try {
			Thread.sleep(60000);
		} catch (InterruptedException e) {
			System.out.println("Interrupted, ignored");
		}
		System.out.println("Stop Receiving");
		for (int i=0; i < sensorReceivers.length; i++) {
			sensorReceivers[i].stopReceiving();
		}
	}
}
